package com.ruoyi.web.controller.task;

import com.ruoyi.common.exception.base.BaseException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.task.domain.SceneCameraParamsInfo;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.task.domain.SceneParamsInfo;
import com.ruoyi.task.service.ISceneParamsInfoService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 场景参数管理Controller
 *
 * @author admin
 * @date 2025-08-20
 */
@RestController
@RequestMapping("/scene")
public class SceneParamsInfoController extends BaseController
{
    @Autowired
    private ISceneParamsInfoService sceneParamsInfoService;

    /**
     * 查询场景参数管理列表
     */
    @PreAuthorize("@ss.hasPermi('scene:params:list')")
    @GetMapping("/params/list")
    public TableDataInfo sceneList(SceneParamsInfo sceneParamsInfo)
    {
        startPage();
        List<SceneParamsInfo> list = sceneParamsInfoService.selectSceneParamsInfoList(sceneParamsInfo);
        return getDataTable(list);
    }

    /**
     * 导出场景参数管理列表
     */
    @PreAuthorize("@ss.hasPermi('scene:params:export')")
    @Log(title = "场景参数管理", businessType = BusinessType.EXPORT)
    @PostMapping("/params/export")
    public void sceneExport(HttpServletResponse response, SceneParamsInfo sceneParamsInfo)
    {
        List<SceneParamsInfo> list = sceneParamsInfoService.selectSceneParamsInfoList(sceneParamsInfo);
        ExcelUtil<SceneParamsInfo> util = new ExcelUtil<SceneParamsInfo>(SceneParamsInfo.class);
        util.exportExcel(response, list, "场景参数管理数据");
    }

    /**
     * 获取场景参数管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('scene:params:query')")
    @GetMapping(value = "/params/{id}")
    public AjaxResult getSceneInfo(@PathVariable("id") Long id)
    {
        return success(sceneParamsInfoService.selectSceneParamsInfoById(id));
    }

    /**
     * 新增场景参数管理
     */
    @PreAuthorize("@ss.hasPermi('scene:params:add')")
    @Log(title = "场景参数管理", businessType = BusinessType.INSERT)
    @PostMapping(value = "/params")
    public AjaxResult addScene(@RequestBody SceneParamsInfo sceneParamsInfo)
    {
        if (StringUtils.isBlank(sceneParamsInfo.getScenariosType())) {
            throw new BaseException("场景类型参数为空");
        }
        sceneParamsInfo.setCreateBy(getUsername());
        sceneParamsInfo.setUpdateBy(getUsername());
        return toAjax(sceneParamsInfoService.insertSceneParamsInfo(sceneParamsInfo));
    }

    /**
     * 修改场景参数管理
     */
    @PreAuthorize("@ss.hasPermi('scene:params:edit')")
    @Log(title = "场景参数管理", businessType = BusinessType.UPDATE)
    @PutMapping(value = "/params")
    public AjaxResult editScene(@RequestBody SceneParamsInfo sceneParamsInfo)
    {
        if (sceneParamsInfo.getId() == null) {
            throw new BaseException("主键参数为空");
        }
        sceneParamsInfo.setUpdateBy(getUsername());
        return toAjax(sceneParamsInfoService.updateSceneParamsInfo(sceneParamsInfo));
    }

    /**
     * 删除场景参数管理
     */
    @PreAuthorize("@ss.hasPermi('scene:params:remove')")
    @Log(title = "场景参数管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/params/{ids}")
    public AjaxResult removeScene(@PathVariable Long[] ids)
    {
        return toAjax(sceneParamsInfoService.deleteSceneParamsInfoByIds(ids));
    }


    /**
     * 查询场景摄像头参数管理列表
     */
    @PreAuthorize("@ss.hasPermi('scene:params:list')")
    @GetMapping("/camera/list")
    public TableDataInfo cameraList(SceneCameraParamsInfo sceneCameraParams)
    {
        startPage();
        List<SceneCameraParamsInfo> list = sceneParamsInfoService.selectSceneCameraParamsList(sceneCameraParams);
        return getDataTable(list);
    }

    /**
     * 导出场景摄像头参数管理列表
     */
    @PreAuthorize("@ss.hasPermi('scene:params:export')")
    @Log(title = "场景摄像头参数管理", businessType = BusinessType.EXPORT)
    @PostMapping("/camera/export")
    public void cameraExport(HttpServletResponse response, SceneCameraParamsInfo sceneCameraParams)
    {
        List<SceneCameraParamsInfo> list = sceneParamsInfoService.selectSceneCameraParamsList(sceneCameraParams);
        ExcelUtil<SceneCameraParamsInfo> util = new ExcelUtil<SceneCameraParamsInfo>(SceneCameraParamsInfo.class);
        util.exportExcel(response, list, "场景摄像头参数管理数据");
    }

    /**
     * 获取场景摄像头参数管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('scene:params:query')")
    @GetMapping(value = "/camera/{id}")
    public AjaxResult getCameraInfo(@PathVariable("id") Long id)
    {
        return success(sceneParamsInfoService.selectSceneCameraParamsById(id));
    }

    /**
     * 新增场景摄像头参数管理
     */
    @PreAuthorize("@ss.hasPermi('scene:params:add')")
    @Log(title = "场景摄像头参数管理", businessType = BusinessType.INSERT)
    @PostMapping(value = "/camera")
    public AjaxResult addCamera(@RequestBody SceneCameraParamsInfo sceneCameraParams)
    {
        if (StringUtils.isBlank(sceneCameraParams.getScenariosType())
                || StringUtils.isBlank(sceneCameraParams.getBankCode())
                || StringUtils.isBlank(sceneCameraParams.getTaskType()) ||
                (sceneCameraParams.getEquipmentId() != null
                        && StringUtils.isBlank(sceneCameraParams.getEquipmentChannel())
                        && StringUtils.isBlank(sceneCameraParams.getCameraId()))) {
            throw new BaseException("参数为空");
        }

        sceneCameraParams.setCreateBy(getUsername());
        sceneCameraParams.setUpdateBy(getUsername());
        return toAjax(sceneParamsInfoService.insertSceneCameraParams(sceneCameraParams));
    }

    /**
     * 修改场景摄像头参数管理
     */
    @PreAuthorize("@ss.hasPermi('scene:params:edit')")
    @Log(title = "场景摄像头参数管理", businessType = BusinessType.UPDATE)
    @PutMapping(value = "/camera")
    public AjaxResult editCamera(@RequestBody SceneCameraParamsInfo sceneCameraParams)
    {
        if (sceneCameraParams.getId() == null) {
            throw new BaseException("主键ID参数为空");
        }
        sceneCameraParams.setUpdateBy(getUsername());
        return toAjax(sceneParamsInfoService.updateSceneCameraParams(sceneCameraParams));
    }

    /**
     * 删除场景摄像头参数管理
     */
    @PreAuthorize("@ss.hasPermi('scene:params:remove')")
    @Log(title = "场景摄像头参数管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/camera/{ids}")
    public AjaxResult removeCamera(@PathVariable Long[] ids)
    {
        return toAjax(sceneParamsInfoService.deleteSceneCameraParamsByIds(ids));
    }
}
